package com.litbw.event;

import com.litbw.dbbackend.DBEntryAdapter;
import com.litbw.user.*;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils.Collections;

import java.util.ArrayList;
import java.util.List;



/**
 * Event class representing a weaving event
 * 
 * @author Silvia Chita
 * @version Feb 13, 2013
 */
public class Contest {
	private int id;
	private int eventParentId;
	private ArrayList<Entry> entries;
	private String description;
	private ArrayList<String> rules;
	private ArrayList<User> judges;
	
	/**
	 * Constructor for a contest.
	 */
	public Contest() {
		entries = new ArrayList<Entry>();
		rules = new ArrayList<String>();
		judges = new ArrayList<User>();
	}
	
	/**
	 * @return The unique ID of this particular contest.
	 */
	public int getId() {
		return id;
	}
	
	/**
	 * Method to set the unique ID of this contest.
	 * @param tId The ID to be set
	 */
	public void setId(final int tId) {
		id = tId;
	}
	
	/**
	 * @return The unique ID of the event parent class that this contest belongs to.
	 */
	public int getEventParentId() {
		return eventParentId;
	}
	
	/**
	 * Method to set the unique ID of the parent event class. Used for moving this contest to
	 * another event.
	 * @param tId The new unique ID to be set.
	 */
	public void setEventParentId(final int tId) {
		eventParentId = tId;
	}
	
	
	/**
	 * @return A copy of the list of entries.
	 */
	public ArrayList<Entry> getEntries() {
		return (ArrayList<Entry>) entries.clone();
	}
	
	/**
	 * @return A copy of the list of judges.
	 */
	public ArrayList<User> getRegistrants() {
		return (ArrayList<User>) judges.clone();
	}
	
	/**
	 * @return A copy of the list of rules for this contest.
	 */
	public ArrayList<String> getCategories() {
		return (ArrayList<String>) rules.clone();
	}
	
	/**
	 * @return A string for the description of the event.
	 */
	public String getDescription() {
		return description;
	}
	
	/**
	 * Method to set the description of this Contest.
	 * @param newDescrip The new description to be set.
	 */
	public void setDescription(String newDescrip) {
		description = newDescrip;
	}

	/**
	 * Method to add an entry to this contest.
	 * @param entry The entry to be added.
	 */
	public void addEntry(Entry entry) {
		entries.add(entry);
	}
	
	/**
	 * Method to add a rule to this contest.
	 * @param newRule The new rule to be added.
	 */
	public void addRule(String newRule) {
		rules.add(newRule);
	}
	
	/**
	 * Method to add a judge to this contest.
	 * @param judge The judge to be added to this contest.
	 */
	public void addJudge(User judge) {
		judges.add(judge);
	}
	
	/**
	 * @return The list of judges that will be judging the contest.
	 */
	public ArrayList<User> getJudges(){
		return judges;
	}
	
	/**
	 * Method to enter a contest as a user with a draft.
	 * @param contestant The contestant entering the contest.
	 * @param the_draft The weaving draft that the user is submitting.
	 * @return True if the entry was accepted, false otherwise.
	 */
	public boolean enterContest(User contestant, Draft the_draft){
		boolean accepted = false;
		if(contestant != null && the_draft  != null){
			Entry newEntry = new Entry(contestant, the_draft);
			addEntry(newEntry);
			accepted = true;
		} 		
		return accepted;		
	}
	
	/**
	 * Returns a list of entries made by a specific user.
	 * @param user The user whose entries will be returned.
	 * @return The list of entries made by a specific user.
	 */
	//TODO: THis needs fixing.
	public List<Entry> getEntries(User user){
		/*
		if(user instanceof User){
			return entries;
		}
		return null;
		*/
		
		// Get entries for user
		Entry[] entries = DBEntryAdapter.getEntriesForContest(id);
		
		List<Entry> all = new ArrayList<Entry>();
		if (entries != null) {
			for (Entry e : entries) {
					if (e.getContestant().getEmail().equals(user.getEmail())) {
					System.out.println("Entry found: " + e.toString());
					all.add(e);
				}
			}
		}
		return all;
	}
	
	/**
	 * Entry class that represents a contest entry with a unique ID. Each entry
	 * has a contestant and a draft. It also has information about whether or not this entry
	 * is a winning entry or if the waiver was signed.
	 */
	public static class Entry {
		private User contestant;
		private Draft draft;
		private List<Integer> contestIds;
		private int id;
		private boolean winner;
		private boolean waiversigned;
		private String description;
		
		/**
		 * Constructor
		 * @param the_contestant The contestant associated with this entry.
		 * @param the_draft The draft submitted for this entry.
		 */
		public Entry(User the_contestant, Draft the_draft){
			contestant = the_contestant;
			draft = the_draft;
			winner = false;
			waiversigned = false;
			contestIds = new ArrayList<Integer>();
		}
		
		/**
		 * Sets the description of this entry.
		 * @param desc The description to be set.
		 */
		public void setDescription(final String desc) {
			description = desc;
		}
		
		/**
		 * @return The description of this entry.
		 */
		public String getDescription() {
			return description;
		}
		
		/**
		 * Sets the waiver signed flag.
		 * @param flag The value to which to set the waiver flag.
		 */
		public void setWaiverSigned(final boolean flag) {
			waiversigned = flag;
		}
		
		/**
		 * @return Whether or not the waiver is signed as a true/false value.
		 */
		public boolean isWaiverSigned() {
			return waiversigned;
		}
		
		/**
		 * Set this entry as a winning entry.
		 * @param flag The flag with which to set whether or not this entry is a winning
		 * 				entry.
		 */
		public void setWinner(final boolean flag) {
			winner = flag;
		}
		
		/**
		 * @return If this entry is a winning entry as a true/false value.
		 */
		public boolean isWinner() {
			return winner;
		}
		
		/**
		 * Sets the ID of the entry to a unique ID.
		 * @param id The new ID to be set.
		 */
		public void setId(final int id) {
			this.id = id;
		}
		
		/**
		 * @return The unique ID of this event.
		 */
		public int getId() {
			return id;
		}
		
		/**
		 * Adds to the list of contests which contests this entry has entered. 
		 * @param id The ID of the contest in which this contest is entered.
		 */
		public void addContestId(final int id) {
			contestIds.add(id);
		}
		
		/**
		 * @return A list of contest IDs representing the contests in which this entry 
		 * 			is partaking.
		 */
		public List<Integer> getContestIds() {
			return contestIds;
		}
		
		/**
		 * @return The draft associated with this entry.
		 */
		public Draft getDraft(){
			return draft;
		}
		
		/**
		 * @return The Contestant associated with this entry.
		 */
		public User getContestant(){
			return contestant;
		}
	}
}
